package com.spelet.screens;


import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.math.Rectangle;
import com.spelet.Main;
import com.spelet.Renderer;
import com.spelet.StaticVariables;
import com.spelet.simulation.Simulation;

public class Game extends Screen {
	public Simulation sim;
	private Main main;
	private float[] startX = new float[5];
	private float[] startY = new float[5];
	private float[] dragX = new float[5];
	private float[] dragY = new float[5];
	Renderer renderer;
	private Rectangle exitRect = new Rectangle(Gdx.graphics.getWidth()-StaticVariables.buttonSize*1.15f,
			StaticVariables.buttonSize*0.15f,StaticVariables.buttonSize,StaticVariables.buttonSize);
	public boolean testing;
	public boolean custom;
	public int level;
	public float timer;
	public Game(Main m, Renderer r) {
		main = m;
		sim = new Simulation(m);
		renderer = r;
	}

	public boolean keyPressed(int key) {			
		return false;
	}
	public void setLevel(int level) {
		this.level = level;
		sim.setLevel(level);
	}
	public boolean keyDown(int key) {
		if (Input.Keys.BACK == key) {
			main.setScreen(Main.SCREEN_MAINMENU);
		}
		
		if (Input.Keys.LEFT == key) {
			if (!sim.character.locked)
				sim.character.moveLeft(sim);
		} else if (Input.Keys.RIGHT == key) {
			if (!sim.character.locked)
				sim.character.moveRight(sim);
		} else if (Input.Keys.UP == key) {
			if (!sim.character.locked)
				sim.character.moveUp(sim);
		} else if (Input.Keys.DOWN == key) {
			if (!sim.character.locked)
				sim.character.moveDown(sim);
		}
		return false;
	}

	public boolean keyTyped(char key) {
		return false;
	}

	public boolean keyUp(int key) {
		return false;
	}

	public boolean scrolled(int arg0) {
		return false;
	}

	public boolean touchDown(int x, int y, int ptr, int arg3) {
		y = Gdx.graphics.getHeight()-y;
		if(exitRect.contains(x,y)) {
			if (this.testing) {
				testing = false;
				main.setScreen(Main.SCREEN_MAPEDITOR);
			} else if (custom) {
				custom= false;
				main.setScreen(Main.SCREEN_MAINMENU);
			} else {
				main.setScreen(Main.SCREEN_MAINMENU);
			}
		} else if(ptr<5) {
			startX[ptr] = x;
			startY[ptr] = y;
			if (startX[ptr] < Gdx.graphics.getWidth()/2) {
				renderer.camLerping = false;
			}
			dragX[ptr] = x;
			dragY[ptr] = y;
		}
		return false;
	}

	public boolean touchDragged(int x, int y, int ptr) {
		if(ptr<5) {
			y = Gdx.graphics.getHeight()-y;
			if (startX[ptr] < Gdx.graphics.getWidth()/2) {
				sim.gameCam.position.set(sim.gameCam.position.x+(dragX[ptr]-x),sim.gameCam.position.y+(dragY[ptr]-y),0);
				if (sim.gameCam.position.x < Gdx.graphics.getWidth()*sim.gameCam.zoom/2)
					sim.gameCam.position.x = Gdx.graphics.getWidth()*sim.gameCam.zoom/2;
				if (sim.gameCam.position.y < Gdx.graphics.getHeight()*sim.gameCam.zoom/2)
					sim.gameCam.position.y = Gdx.graphics.getHeight()*sim.gameCam.zoom/2;
				if (sim.gameCam.position.x > sim.map.cols*64-Gdx.graphics.getWidth()*sim.gameCam.zoom/2)
					sim.gameCam.position.x = sim.map.cols*64-Gdx.graphics.getWidth()*sim.gameCam.zoom/2;
				if (sim.gameCam.position.y > sim.map.rows*64-Gdx.graphics.getHeight()*sim.gameCam.zoom/2)
					sim.gameCam.position.y = sim.map.rows*64-Gdx.graphics.getHeight()*sim.gameCam.zoom/2;				
			}
			dragX[ptr] = x;
			dragY[ptr] = y;
		}
		return false;
	}

	public boolean touchMoved(int arg0, int arg1) {

		return false;
	}

	public boolean touchUp(int x, int y, int ptr, int arg3) {
		if(ptr<5)
			move(x, Gdx.graphics.getHeight()-y, ptr);
		return false;
	}

	public void update() {
		this.timer += Gdx.graphics.getDeltaTime()*1000;
		sim.update();
	}

	public void move(float endX, float endY, int ptr){
		if(startX[ptr] >= Gdx.graphics.getWidth()/2){
			StaticVariables.moveDirection.set(endX-startX[ptr], endY-startY[ptr]);
			if(StaticVariables.moveDirection.len2() >= 5000){
				if (!sim.character.locked){
					float angle = StaticVariables.moveDirection.angle();
					if((angle >= 0 && angle <= 45) || (angle <= 360 && angle >= 315)){
						sim.character.moveRight(sim);
					} else if((angle <= 135 && angle >= 45)){
						sim.character.moveUp(sim);
					} else if(angle <= 315 && angle >= 225){
						sim.character.moveDown(sim);
					} else if(angle >= 135 && angle <= 225){
						sim.character.moveLeft(sim);
					}
		
					if (!renderer.camRect.contains(sim.character.spriteX, sim.character.spriteY))
						renderer.camLerping = true;
				}
			}
		}
	}

	public void restart() {
	}

	public void enter() {
		Gdx.input.setInputProcessor(this);
		Gdx.input.setCatchBackKey(true);
		timer = 0;
	}
	public void render() {
		renderer.renderGame(this, sim);
	}

	public void customLevel(String readString) {
		sim.customLevel(readString);
		custom = true;
		testing = false;
	}

	public void testLevel(String string) {
		sim.testLevel(string);
		custom = false;
		testing = true;
	}
}